package com.itcam.bubblesort;

import java.util.Arrays;

/**
 * @author : Cammy.Wu
 * Description : 
 */

public class BubbleSort {
    public static void main(String[] args) {
        int[] a = {9, 5, 7, 12, 1, 3};
        System.out.println(Arrays.toString(a));
        bubbleSort(a, a.length - 1);
        System.out.println(Arrays.toString(a));
    }

    private static void bubbleSort(int[] arr, int n) {
        for (int i = 0; i < n; i++) {
            boolean flag = false;
            for (int j = 0; j < n - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    swap(arr, j);
                    flag = true;
                }
            }
            if (!flag) {
                break;
            }
        }
    }

    private static void swap(int[] arr, int j) {
        int temp = arr[j];
        arr[j] = arr[j + 1];
        arr[j + 1] = temp;
    }
}
